/*
 * @Author: 朝言夕识 11442050+we-know-each-other-every-day@user.noreply.gitee.com
 * @Date: 2024-07-06 19:47:45
 * @LastEditors: 朝言夕识 11442050+we-know-each-other-every-day@user.noreply.gitee.com
 * @LastEditTime: 2024-07-06 20:06:38
 * @FilePath: \小兔仙项目\vue-rabbit\src\composables\useCountDown.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//封装倒计时逻辑
import { computed, ref, onUnmounted } from 'vue'
import dayjs from 'dayjs'

export const useCountDown = () => {
    let timer = null
    const time = ref(0)

    const formatTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))

    const start = (currentTime) => {
        time.value = currentTime
        setInterval(() => {
            time.value--
        }, 1000)
    }

    //组件销毁时，清除定时器
    onUnmounted(() => {
        timer && clearInterval(timer)
    })
    return {
        formatTime,
        start
    }
}